package com.nercel.dsj.gksales.config;

import com.nercel.dsj.gksales.interceptor.GlobalAccessInterceptor;
import com.nercel.dsj.gksales.interceptor.ReturnHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @author sunlin
 * @date 2018-10-30
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Value("${app.upload.path}")
    private String uploadPath;

    /**
     * 注册参数解析器
     * @param argumentResolvers
     */
    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        /**
         * 注册自定义参数解析器 解析自定的分页及排序参数
         */
        PageHandArgumentResolver pageHandArgumentResolver = new PageHandArgumentResolver();
        argumentResolvers.add(pageHandArgumentResolver);
        super.addArgumentResolvers(argumentResolvers);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/temp/**")
                .addResourceLocations("file:" + uploadPath + "/temp/");
        registry.addResourceHandler("/import-templates/**")
                .addResourceLocations("file:" + uploadPath + "/import-templates/");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 添加跨域支持配置
        registry.addMapping("/**")
                .allowedOrigins("*")
                //.allowCredentials(true)
                .allowedMethods("PUT", "POST", "GET", "DELETE", "OPTIONS")
                .allowedHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "token", "Authorization")
                .maxAge(3600);
    }

    @Override
    protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        ReturnHandler returnHandler = new ReturnHandler();
        returnValueHandlers.add(returnHandler);
        super.addReturnValueHandlers(returnValueHandlers);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(globalAccessInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
    @Bean
    public GlobalAccessInterceptor globalAccessInterceptor(){
        return new GlobalAccessInterceptor();
    }

}
